//
//  SelectCityView.m
//  Molian
//
//  Created by xiaocankeji on 16/2/2.
//  Copyright © 2016年 xiaocan. All rights reserved.
//

#import "SelectCityView.h"
#import "UIButton+WebCache.h"
#import "SelectCityBt.h"
#define kBtTag 133
#define kTopStart 10
#define kBtHeight 110 
#define kBtWidth  80
@interface SelectCityView ()
@property (nonatomic, strong) UIScrollView *contentView;
@property (nonatomic) BOOL isQuestCity;//请求过
@property (nonatomic, strong) NSArray *cityArr;
@end

@implementation SelectCityView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
         self.alpha = 0;
         self.backgroundColor = RGBA(1, 1, 1, 0.3);
        
        [self foundContentView];
        
    }
   
    return self;
}
- (void)foundContentView
{
    [self.contentView removeFromSuperview];
   // self.cityArr = [Utils getCityList];
    if (self.cityArr&&self.cityArr.count) {
        
        CGFloat h = kBtHeight*((self.cityArr.count - 1)/3+1);
        if (h>DeviceHeight) {
            self.contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, DeviceWidth, DeviceHeight)];
        }else{
           self.contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, DeviceWidth, h)];
        }
        [self.contentView setContentSize:CGSizeMake(DeviceWidth, h)];
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.contentView];
        CGFloat leftSpacing = (DeviceWidth - kBtWidth*3)/6;
        
        for (int i = 0; i<self.cityArr.count; i++) {
            
            CGFloat _x = (i%3)*(kBtWidth+2*leftSpacing);
            CGFloat _y = (i/3)*kBtHeight;
            NSDictionary *cityDic = [self.cityArr objectAtIndex:i];
            NSString *cityName = [cityDic objectForKey:@"cityName"];
            NSString *cityUrl = [cityDic objectForKey:@"cityUrl"];
            
            SelectCityBt *cityBt = [[SelectCityBt alloc] initWithFrame:CGRectMake(leftSpacing + _x, kTopStart+_y, kBtWidth, kBtHeight)];
           
            cityBt.titleContentLabel.text = cityName;
            [cityBt.topImageView sd_setImageWithURL:[NSURL URLWithString:cityUrl]];
            cityBt.tag = kBtTag+i;
            [cityBt addTarget:self action:@selector(clickCityBt:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:cityBt];
        }
        
        
        
        if (self.isQuestCity == NO) {
            [self questCity];
        }
        
        
        
    }else{
        [self questCity];
    }
    
}
- (void)clickCityBt:(UIButton *)bt
{
    [Utils Hidden:self Bool:NO];
    if([self.delegate respondsToSelector:@selector(clickCity:)]){
        NSDictionary *dic = [self.cityArr objectAtIndex:bt.tag - kBtTag];
        NSString *cityName = [dic objectForKey:@"cityName"];
        [self.delegate clickCity:cityName];
    }
    
    
    
}
- (void)questCity
{
    self.isQuestCity = YES;
//    AVQuery *q = [City query];
//    [q orderByAscending:@"priority"];
//    
//   // [HUD setOnViewWithTitle:nil animated:YES];
//    [q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
//     //   [HUD hideFromViewAnimated:YES];
//        if (!error&&objects&&objects.count>0) {
//            NSMutableArray *arr = [NSMutableArray array];
//            for (City *city in objects) {
//                NSDictionary *dic= [NSDictionary dictionaryWithObjectsAndKeys:city.cityName,@"cityName",city.cityFile.url,@"cityUrl", nil];
//                [arr addObject:dic];
//            }
//            [Utils saveCityListArr:[NSArray arrayWithArray:arr]];
//            [self foundContentView];
//        }
//        
//        
//    }];
}
- (void)dealloc
{
    self.delegate = nil;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
